import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:openim/routes/app_navigator.dart';
import 'package:openim_common/openim_common.dart';

import '../login/login_logic.dart';

class ForgetPasswordLogic extends GetxController {
  final phoneCtrl = TextEditingController();
  final verificationCodeCtrl = TextEditingController();
  final areaCode = "+86".obs;
  final enabled = false.obs;
  final loginController = Get.find<LoginLogic>();
  String? get email => loginController.operateType == LoginType.email ? phoneCtrl.text.trim() : null;
  String? get phone => loginController.operateType == LoginType.phone ? phoneCtrl.text.trim() : null;
  @override
  void onClose() {
    phoneCtrl.dispose();
    verificationCodeCtrl.dispose();
    super.onClose();
  }

  @override
  void onInit() {
    phoneCtrl.addListener(_onChanged);
    verificationCodeCtrl.addListener(_onChanged);
    super.onInit();
  }

  _onChanged() {
    enabled.value = phoneCtrl.text.trim().isNotEmpty && verificationCodeCtrl.text.trim().isNotEmpty;
  }

  void openCountryCodePicker() async {
    String? code = await IMViews.showCountryCodePicker();
    if (null != code) areaCode.value = code;
  }

 Future<bool> getVerificationCode() async {
  debugPrint('getVerificationCode: $phone, $email, $areaCode');
  
  // 手机号登录类型的校验
  if (loginController.operateType == LoginType.phone) {
    if (phone == null || phone!.isEmpty) {
      IMViews.showToast(StrRes.plsEnterRightPhone);
      return false;
    }
    if (!IMUtils.isMobile(areaCode.value, phone!)) {
      IMViews.showToast(StrRes.plsEnterRightPhone);
      return false;
    }
  }
  
  // 邮箱登录类型的校验
  if (loginController.operateType == LoginType.email) {
    if (email == null || email!.isEmpty) {
      IMViews.showToast(StrRes.invalidEmailFormat);
      return false;
    }
    if (!IMUtils.isValidEmail(email!)) {
      IMViews.showToast(StrRes.invalidEmailFormat);
      return false;
    }
  }

  final success = await sendVerificationCode();
  return success;
}

  Future<bool> sendVerificationCode() => LoadingView.singleton.wrap(
      asyncFunction: () {
        // 根据登录类型选择不同的验证码发送接口
        if (loginController.operateType == LoginType.email) {
          return Apis.sendEmailVerificationCode(
            email: email!,
          );
        } else {
          return Apis.requestVerificationCode(
            phone: phone ?? '',
            areaCode: areaCode.value,
          );
        }
      });

  checkVerificationCode() => LoadingView.singleton.wrap(
      asyncFunction: () {
        // 根据登录类型选择不同的验证码验证接口
        if (loginController.operateType == LoginType.email) {
          return Apis.checkEmailVerificationCode(
            email: email!,
            verificationCode: verificationCodeCtrl.text,
          );
        } else {
          return Apis.checkVerificationCode(
            areaCode: areaCode.value,
            phoneNumber: phone,
            email: email,
            verificationCode: verificationCodeCtrl.text,
            usedFor: 2,
          );
        }
      });

  void nextStep() async {
    await checkVerificationCode();
    AppNavigator.startResetPassword(
      areaCode: areaCode.value,
      phoneNumber: phone,
      email: email,
      verificationCode: verificationCodeCtrl.text,
    );
  }
}
